package com.chzsyh.evwer.vm;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建日期 2020/11/6
 * 描述：获取手机的apk信息
 */
public class ApkTool {
    private static String TAG = "ApkTool";

    public static void getPackages(Context context) {
        // 获取已经安装的所有应用, PackageInfo　系统类，包含应用信息
        List<PackageInfo> packages = context.getPackageManager().getInstalledPackages(0);
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < packages.size(); i++) {
            PackageInfo packageInfo = packages.get(i);
            Map<String, Object> map = new HashMap<>();
            map.put("appName", packageInfo.applicationInfo.loadLabel(context.getPackageManager()).toString());
            map.put("appVersion", packageInfo.versionName);
            map.put("packageName", packageInfo.packageName);
            map.put("appLatestUpdateTime", packageInfo.lastUpdateTime);
            map.put("appInstallTime", packageInfo.firstInstallTime);
            if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { //非系统应用
                // AppInfo 自定义类，包含应用信息
                map.put("appType", 1);
                /*appInfo.setAppName(
                        packageInfo.applicationInfo.loadLabel(getPackageManager()).toString());//获取应用名称
                appInfo.setPackageName(packageInfo.packageName); //获取应用包名，可用于卸载和启动应用
                appInfo.setVersionName(packageInfo.versionName);//获取应用版本名
                appInfo.setVersionCode(packageInfo.versionCode);//获取应用版本号
                appInfo.setAppIcon(packageInfo.applicationInfo.loadIcon(getPackageManager()));//获取应用图标*/
            } else { // 系统应用
                map.put("appType", 0);
//                Log.d(TAG, "getPackages: "+ DateTime.format(calendar,"yyyy-MM-dd HH:mm:ss"));

            }
            list.add(map);
        }
        Log.d(TAG, "getPackages:list =  " + list);
    }
}
